package com.sie.demo.controller;

import com.sie.demo.entity.Menu;
import com.sie.demo.entity.RetResponse;
import com.sie.demo.entity.RetResult;
import com.sie.demo.entity.vo.PageMenuVo;
import com.sie.demo.entity.vo.PageRoleVo;
import com.sie.demo.entity.vo.QueryMenuVo;
import com.sie.demo.service.MenuService;
import com.sie.demo.service.RoleMenuService;
import com.sie.demo.service.RoleService;
import com.sie.demo.service.impl.MenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author liaozhifeng
 * @date 2021/8/8 19:21
 * @Version 1.0
 */

@RequestMapping("menu")
@RestController
public class MenuController {

    @Autowired
    MenuServiceImpl menuService;
    @Autowired
    RoleMenuService roleMenuService;
    @GetMapping("selectAll")
    public RetResult<PageMenuVo> selectAll(long currentPage, long pageSize, QueryMenuVo queryMenuVo) {
        PageMenuVo pageUserVo  = menuService.selectAll(currentPage,pageSize,queryMenuVo);
        return RetResponse.makeOKRsp(pageUserVo);
    }


    @PostMapping("add")
    RetResult<Menu> addUser(Menu menu) {
//        获取当前系统时间
        Date date = new Date();
//        添加到Oracle数据库中的时间格式为：【 年/月/日  时:分:秒 】
        java.sql.Timestamp createTime = new java.sql.Timestamp(date.getTime());
        menu.setCreateTime(createTime);
        menu.setUpdateTime(createTime);
        int i = menuService.addMenu(menu);
        if (i == 1) {
            return RetResponse.makeOKRsp("插入成功", menu, "");
        } else {
            return RetResponse.makeRsp(500, "服务器异常");
        }
    }

    @PostMapping("edit")
    public RetResult<Menu> editUser(Menu menu) {
        //        获取当前系统时间
        Date date = new Date();
//        添加到Oracle数据库中的时间格式为：【 年/月/日  时:分:秒 】
        java.sql.Timestamp updateTime = new java.sql.Timestamp(date.getTime());
        menu.setUpdateTime(updateTime);
        int i = menuService.editMenu(menu);
        if (i == 1) {
            return RetResponse.makeOKRsp("修改成功", menu, "");
        } else {
            return RetResponse.makeRsp(500, "服务器异常");
        }
    }

    @GetMapping("selectAllMenu")
    public RetResult< List> selectAllMenu() {
        List<Menu> list = menuService.handleMenu();
        return RetResponse.makeOKRsp(list);
    }
    @GetMapping("selectMenuByRoleId")
    public RetResult<List> selectMenuByRoleId(Integer roleId) {
        List<Integer> list = roleMenuService.selectRoleByUserId(roleId);
        return RetResponse.makeOKRsp(list);
    }

    @DeleteMapping("delete")
    RetResult<Menu> deleteRoles(Integer menuId){
        Integer isDelete = menuService.deleteMenuByIds(menuId);
        if (isDelete != 0){
            return RetResponse.makeRsp(200,"删除成功");
        }else {
            return RetResponse.makeRsp(500,"删除失败");
        }
    }


}
